commonlibsse_ng\re\e/
ExtraReferenceHandle.rs

1//! # ExtraReferenceHandle
2//!
3//! Represents extra data for reference handles.
4//!
5//! Inherits from `BSExtraData` and includes a reference handle and padding for alignment.
6//!
7//! # Memory Layout:
8//! - `__base`: Base class `BSExtraData`
9//! - `container_ref`: The object reference handle (0x10)
10//! - `pad14`: Padding for alignment (0x14)
11
12use crate::re::BSExtraData::{BSExtraData, DerivedBSExtraData};
13use crate::re::BSPointerHandle::ObjectRefHandle;
14use crate::re::ExtraDataType::ExtraDataType;
15use crate::re::NiSmartPointer::NiPointer;
16use crate::re::TESObjectREFR::TESObjectREFR;
17use crate::re::offsets_rtti::RTTI_ExtraReferenceHandle;
18use crate::re::offsets_vtable::VTABLE_ExtraReferenceHandle;
19use crate::rel::id::VariantID;
20
21#[repr(C)]
22#[derive(Debug, PartialEq)]
23pub struct ExtraReferenceHandle {
24    /// Base class `BSExtraData`.
25    pub __base: BSExtraData,
26
27    /// The object reference handle.
28    /// Offset: `0x10`
29    pub container_ref: ObjectRefHandle,
30
31    /// Padding to match C++ structure alignment.
32    /// Offset: `0x14`
33    pub pad14: u32,
34}
35
36const _: () = {
37    assert!(core::mem::offset_of!(ExtraReferenceHandle, __base) == 0x0);
38    assert!(core::mem::offset_of!(ExtraReferenceHandle, container_ref) == 0x10);
39    assert!(core::mem::offset_of!(ExtraReferenceHandle, pad14) == 0x14);
40    assert!(core::mem::size_of::<ExtraReferenceHandle>() == 0x18);
41};
42
43impl Default for ExtraReferenceHandle {
44    #[inline]
45    fn default() -> Self {
46        Self::new()
47    }
48}
49
50impl DerivedBSExtraData for ExtraReferenceHandle {
51    #[inline]
52    fn get_extra_data(&self) -> &BSExtraData {
53        &self.__base
54    }
55
56    #[inline]
57    fn get_extra_data_type() -> ExtraDataType {
58        Self::EXTRA_DATA_TYPE
59    }
60}
61
62impl ExtraReferenceHandle {
63    /// Address & offset of the runtime type information (RTTI) identifier.
64    pub const RTTI: VariantID = RTTI_ExtraReferenceHandle;
65
66    /// Address & offset of the virtual function table.
67    pub const VTABLE: [VariantID; 1] = VTABLE_ExtraReferenceHandle;
68
69    /// The `ExtraDataType` value for reference handles.
70    pub const EXTRA_DATA_TYPE: ExtraDataType = ExtraDataType::ReferenceHandle;
71
72    /// Creates a new `ExtraReferenceHandle` instance with a null reference.
73    #[inline]
74    pub const fn new() -> Self {
75        Self { __base: BSExtraData::new(), container_ref: ObjectRefHandle::new(), pad14: 0 }
76    }
77
78    /// Creates a new `ExtraReferenceHandle` with a specific reference handle.
79    #[inline]
80    pub const fn from_handle(container_ref: ObjectRefHandle) -> Self {
81        Self { __base: BSExtraData::new(), container_ref, pad14: 0 }
82    }
83
84    /// Gets the extra data type, always returning `ExtraDataType::ReferenceHandle`.
85    #[inline]
86    pub const fn get_type(&self) -> ExtraDataType {
87        ExtraDataType::ReferenceHandle
88    }
89
90    /// Checks if this `ExtraReferenceHandle` is not equal to another.
91    #[inline]
92    pub fn is_not_equal(&self, rhs: &Self) -> bool {
93        self.container_ref != rhs.container_ref
94    }
95
96    /// Retrieves the original reference as `NiPointer<TESObjectREFR>`.
97    #[inline]
98    pub fn get_original_reference(&self) -> NiPointer<TESObjectREFR> {
99        self.container_ref.get()
100    }
101}
102
103/// The virtual function table for `ExtraReferenceHandle`.
104///
105/// This struct defines function pointers to simulate the C++ virtual functions.
106#[repr(C)]
107#[derive(Debug)]
108pub struct ExtraReferenceHandleVtbl {
109    /// Destructor function pointer.
110    pub CxxDrop: fn(this: &mut ExtraReferenceHandle),
111
112    /// Function pointer for retrieving the extra data type.
113    pub GetType: fn(this: &ExtraReferenceHandle) -> ExtraDataType,
114
115    /// Function pointer for equality check.
116    pub IsNotEqual: fn(this: &ExtraReferenceHandle, rhs: &ExtraReferenceHandle) -> bool,
117}
118
119impl Default for ExtraReferenceHandleVtbl {
120    #[inline]
121    fn default() -> Self {
122        Self::new()
123    }
124}
125
126impl ExtraReferenceHandleVtbl {
127    /// Creates a new default virtual table with stubbed functions.
128    pub const fn new() -> Self {
129        const fn CxxDrop(_this: &mut ExtraReferenceHandle) {}
130
131        const fn GetType(_this: &ExtraReferenceHandle) -> ExtraDataType {
132            ExtraReferenceHandle::EXTRA_DATA_TYPE
133        }
134
135        fn IsNotEqual(this: &ExtraReferenceHandle, rhs: &ExtraReferenceHandle) -> bool {
136            this.container_ref != rhs.container_ref
137        }
138
139        Self { CxxDrop, GetType, IsNotEqual }
140    }
141}